function onLoad(){
	document.title=window.arguments[0];
	loadBuilds();
	loadTroops();
	loadTechs();
	// Add events
	var xpr=document.evaluate("//*[contains(@class,'sort')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	for(var i=0; item=xpr.snapshotItem(i); i++){
		item.addEventListener("click",Sort,false);
	}
	// Size of view
	var i;
	var size=wind().get_var("ccentersize");
	var trees=document.getElementsByTagName("tree");
	for(i=0; i<trees.length; i++){
		trees[i].setAttribute("rows",size);
	}
}

function Sort(e){
	var i;
	if(e.nodeName!="treecol") return;
	var dir=false; // False = ASC && True = DES
	var col=e.getAttribute("id").match(/\d+/)[0];
	var tree=e.parentNode.parentNode;
	var rows=tree.getElementsByTagName("treeitem");
	var sand=document.getElementById("sandbox");
	if(tree.getAttribute("sortResource")=="c"+col){
		// Invert direction
		dir=!dir;
	}
	if(rows.length>0){
		// Move first to sandbox
		sand.appendChild(rows[rows.length-1]);
		// For each other row
		for(i=rows.length-2; i>=0; i--){
			// Compare to each in sandbox
			var cursor=-1;
			while(cursor==-1 || (((!dir && curV>pointer) || (dir && curV<pointer)) && cursor<sand.childNodes.length-1)){
				cursor++;
				curV=rows[i].childNodes[0].childNodes[col].getAttribute("label");
				pointer=sand.childNodes[cursor].childNodes[0].childNodes[col].getAttribute("label");
			}
			// Now append to sandbox
			sand.insertBefore(rows[i],sand.childNodes[cursor]);
		}
		// Reput in the same order on the box
		for(i=0; i<sand.childNodes.length; i++){
			tree.getElementsByTagName("treeitem")[0].appendChild(sand.childNodes[i]);
		}
	}
	// Update registers on tree for reverse and save
	tree.setAttribute("sortDirection",dir);
	tree.setAttribute("sortResource","c"+col);
}

function wind(){
	return window.arguments[1].TWB;
}

function openSub(list){
	var i;
	var vb=document.getElementById('groupbox').childNodes;
	for(i=0; i<vb.length; i++){
		vb[i].setAttribute("hidden",true)
	}
	document.getElementById("groupbox_"+list).setAttribute("hidden",false);
}

function clearAll(list){
	var i;
	list=document.getElementById(list).getElementsByTagName("treeitem");
	for(i=0; i<list.length; i++){
		list[i].parentNode.removeChild(list[i]);
	}
}

function addItem(item,list){
	var i;
	var screen;
	switch(list){
		case "builds": screen="main"; break;
		case "troops": screen="place"; break;
		case "techs": screen="smith"; break;
		default : screen="overview"; break;
	}
	list=document.getElementById(list);
	var ael=document.createElement("treeitem");
	var el=document.createElement("treerow");
	el.setAttribute("id",item[0]);
	el.setAttribute("name",screen);
	el.addEventListener("dblclick",clickThrough,false);
	for(i=1; i<item.length; i++){
		var el_=document.createElement("treecell");
		el_.setAttribute("label",item[i]);
		el_.className="calign";
		el.appendChild(el_);
	}
	ael.appendChild(el);
	list.appendChild(ael);
}

function clickThrough(e){
	e=e.target;
	win().Modules.Villages.MakeUrl(e.getAttribute("id"),e.getAttribute("name"));
	var world=wind().World(true);
	if(typeof wind().Servers[world.replace(/\d+/,"")]=="undefined"){
		return alert(wind().lang("servernotsup"));
	}
}

function loadBuilds(){
	clearAll('builds');
	_BUILDS=wind()._SESSION.BUILDS;
	var vil_id;
	for(vil_id in _BUILDS){
		var it=_BUILDS[vil_id];
		addItem([
			vil_id,
			wind().gei("TWB-v"+vil_id).getAttribute('value'),
			it.main,
			it.church || it.church_f,
			it.barracks,
			it.stable,
			it.garage,
			it.smith,
			it.snob,
			it.place,
			it.statue,
			it.market,
			it.wood,
			it.stone,
			it.iron,
			it.farm,
			it.storage,
			it.hide,
			it.wall
		],'builds');
	}
}

function loadTroops(){
	clearAll('troops');
	_UNITS=wind()._SESSION.UNITS;
	var vil_id;
	for(vil_id in _UNITS){
		var it=_UNITS[vil_id];
		addItem([
			vil_id,
			wind().gei("TWB-v"+vil_id).getAttribute('value'),
			it.spear,
			it.sword,
			it.axe,
			it.archer,
			it.spy,
			it.light,
			it.marcher,
			it.heavy,
			it.ram,
			it.catapult,
			it.knight,
			it.snob
		],'troops');
	}
}

function loadTechs(){
	clearAll('techs');
	_TECHS=wind()._SESSION.TECHS;
	var vil_id;
	for(vil_id in _TECHS){
		var it=_TECHS[vil_id];
		addItem([
			vil_id,
			wind().gei("TWB-v"+vil_id).getAttribute('value'),
			it.spear,
			it.sword,
			it.axe,
			it.archer,
			it.spy,
			it.light,
			it.marcher,
			it.heavy,
			it.ram,
			it.catapult
		],'techs');
	}
}